/**
 * 
 */
package PyroBots.Vue;


import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import PyroBots.Controlleur.CSimulateur;
import PyroBots.Controlleur.CTerrain;
import PyroBots.Modele.Simulateur;
import PyroBots.Vue.Listeners.ButtonPlayListener;
import PyroBots.Vue.Listeners.ButtonStopListener;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author glen
 * @generated "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class VueSimu extends JPanel implements Observer {
	
	private int nbFeux;
	private int feuxEteint;
	private CSimulateur cSimu = null;
	private JLabel jLabel_chrono = null;
	private JProgressBar JPB_progressChrono = null;
	private JSlider JSB_vitesse = null;
	private ButtonFlipFlop buttonFlipFlop_play = null;
	private JButton jButton_stop = null;
	private VueLog vueLog = null;
	
	public VueSimu (CSimulateur cSimu,VueLog v){	
		this.cSimu = cSimu;
		this.cSimu.setVueSimu(this);
		this.nbFeux = 0;
		this.feuxEteint = 0;
		
		//this.setIcon(new ImageIcon(getClass().getResource("/img/pause.png")));
		jLabel_chrono = new JLabel();
		jLabel_chrono.setFont(new Font("Dialog", Font.BOLD, 36));
		jLabel_chrono.setForeground(Color.black);
		jLabel_chrono.setHorizontalAlignment(SwingConstants.CENTER);
		jLabel_chrono.setBackground(Color.black);
		jLabel_chrono.setText("00:00");
		
		JPB_progressChrono = new JProgressBar();
		JPB_progressChrono.setMinimum(0);
		JPB_progressChrono.setMaximum(nbFeux);
		JPB_progressChrono.setValue(30);
		
		
		
		JSB_vitesse = new JSlider();
		JSB_vitesse.setPaintTicks(true);
		JSB_vitesse.setMajorTickSpacing(25);
		JSB_vitesse.setMinorTickSpacing(5);
		JSB_vitesse.setSnapToTicks(true);
		JSB_vitesse.setPaintLabels(true);
		JSB_vitesse.setValue(100);
		JSB_vitesse.setMaximum(100);
		JSB_vitesse.addChangeListener(new SliderListener(this.cSimu));
		
		buttonFlipFlop_play = new ButtonFlipFlop();
		buttonFlipFlop_play.setSelected(true);
		buttonFlipFlop_play.setMnemonic(KeyEvent.VK_UNDEFINED);
		buttonFlipFlop_play.setPreferredSize(new Dimension(32, 32));
		
		jButton_stop = new JButton();
		jButton_stop.setIcon(new ImageIcon(getClass().getResource("/img/stop.png")));
		jButton_stop.setSelected(true);
		jButton_stop.setMnemonic(KeyEvent.VK_UNDEFINED);
		jButton_stop.setPreferredSize(new Dimension(32, 32));
		
		this.vueLog = v;
		
		FlowLayout flowLayout1 = new FlowLayout();
		flowLayout1.setAlignment(FlowLayout.CENTER);
		flowLayout1.setVgap(30);
		this.setLayout(flowLayout1);
		this.setBounds(new Rectangle(14, 337, 205, 291));
		this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		this.add(jLabel_chrono, null);
		this.add(JPB_progressChrono, null);
		this.add(JSB_vitesse, null);
		this.add(buttonFlipFlop_play, null);
		ButtonPlayListener buttonPlayStop = new ButtonPlayListener(this.cSimu, buttonFlipFlop_play,this);
		buttonFlipFlop_play.addMouseListener(buttonPlayStop);
		this.add(jButton_stop, null);
		jButton_stop.addMouseListener(new ButtonStopListener(cSimu, buttonFlipFlop_play,this,v));
	}
	
	

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		int s = (int) (this.cSimu.getSimulateur().getSeconds() /10);
		int min = s/60;
		int sec = s%60;
		jLabel_chrono.setText(min+":"+sec);
		
		if (arg != null)
		{

			if (arg.getClass().getName() == "java.lang.Boolean"){
				if ((Boolean) arg){
					this.setFeuxEteint(this.feuxEteint+1);
				}
			}	
			if (arg.getClass().getName() == "java.lang.Integer"){
				int nb = (Integer) arg;
				if (this.getNbFeux() == 0)this.setNbFeux(nb);
		}
		}
	}
	
	public void setjButton_play(ButtonFlipFlop buttonFlipFlop_play) {
		this.buttonFlipFlop_play = buttonFlipFlop_play;
	}

	public ButtonFlipFlop getjButton_play() {
		return this.buttonFlipFlop_play;
	}
	
	class SliderListener implements ChangeListener {

		private CSimulateur cSimu;
		public SliderListener(CSimulateur p_cSimu){
			cSimu = p_cSimu;
		}
		@Override
		public void stateChanged(ChangeEvent e) {
			// TODO Module de remplacement de m�thode auto-g�n�r�
			 JSlider source = (JSlider)e.getSource();
		        if (!source.getValueIsAdjusting()) {
		            int p = (int)source.getValue();
		            this.cSimu.modifierTimer(p);
		            
		        }    
		}
		
		
	}


	public JProgressBar getJPB_progressChrono() {
		return JPB_progressChrono;
	}

	public JSlider getJSB_vitesse() {
		return JSB_vitesse;
	}

	public int getNbFeux() {
		return nbFeux;
	}

	public void setNbFeux(int nbFeux) {
		this.nbFeux = nbFeux;
		this.JPB_progressChrono.setMaximum(nbFeux);
	}

	public int getFeuxEteint() {
		return feuxEteint;
	}

	public void setFeuxEteint(int feuxEteint) {
		this.feuxEteint = feuxEteint;
		this.JPB_progressChrono.setValue(feuxEteint);
	}
	
	public void reinit() {
		setFeuxEteint(0);
		setNbFeux(0);
		jLabel_chrono.setText("00:00");
	}
	
}


